import Vue from 'vue';
import Request from "@/sdk/pocky-request/index.js";
var vm = new Vue();
/**
 * 用户模块store
 */
const user = {
	// 状态
	state: {
		isLogin: false,
		userInfo: null,
		token: null,
		parentId:0
	},
	// 内部派生
	getters: {

	},
	// 同步方法 commit
	mutations: {
		isLogin: (state, payload) => {
			state.isLogin = payload
		},
		userInfo: (state, payload) => {
			state.userInfo = payload
		},
		parentId: (state, payload) => {
			state.parentId = payload
		},
		token: (state, payload) => {
			state.token = payload;
			uni.setStorageSync('token', payload)
		},
		eidtTel: (state, payload) => {
			state.userInfo.username = payload
		}
	},
	// 异步方法 dispatch
	actions: {
		/**
		 * @param {Object} context
		 * @param {Object} _data
		 * _data:{
			 username
			 password
		 }
		 * 
		 */
		apiUserLogin(context, _data) {
			return new Promise(resolve => {
				Request().request({
					url: '/common/login.do',
					method: 'post',
					data: _data
				}).then(res => {
					
					if (res.data.code == 1) {
						this.commit("userInfo", res.data.data);
						this.commit("token", res.data.data.token);
						this.commit("isLogin", true);
						resolve(res)
					} else {
						uni.showToast({
							title: '登录失败,' + res.data.msg,
							icon: 'none'
						});
					}
				})
			})
		},
		/**
		 * @description  自动登录
		 * @param {Object} context
		 * @param {Object} _data:{token:'xxxxx'}
		 */
		apiUserAutoLogin(context, _data) {
			return new Promise((resolve, reject) => {
				
				Request().request({
					url: '/common/login/auto.do',
					method: 'post',
					data: _data
				}).then(res => {
					
					if (res.data.code == 1) {
						this.commit("userInfo", res.data.data);
						this.commit("token", res.data.data.token);
						this.commit("isLogin", true);
						resolve(res)
					} else {
						uni.showToast({
							title: '您的登录信息已过期,请重新登录',
							icon: 'none'
						});
						reject(res)
					}
				})
			})
		},
		/**
		 * @description 用户注册
		 * @param {Object} context
		 * @param {Object} _data
		 * _data:{
			"username"
			"password"
			"code":
		 }
		 */
		apiUserRegister(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/common/regist.do',
					method: 'post',
					data: _data
				}).then(res => {
					
					if (res.data.code == 1) {
						this.commit("userInfo", res.data.data);
						this.commit("token", res.data.data.token);
						this.commit("isLogin", true);
						resolve(res)
					} else {
						uni.showToast({
							title: '注册失败',
							icon: 'none'
						});
						reject(res)
					}
				})
			})
		},
		/**
		 * @description 发送短信验证码
		 * @param {Object} context
		 * @param {Object} _data
		 * //1.注册 2.登录 3.修改密码 4.验证原手机号 5.修改手机号
		 */
		apiSendMsgCode(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/common/send_sms.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)
					} else {
						uni.showToast({
							title: '验证码发送失败,' + res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				}).catch(err => {
					uni.showToast({
						title: '验证码发送失败,' + res.data.msg,
						icon: 'none'
					});
					reject(res)
				})
			})
		},
		/**
		 * @description 修改密码
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiEditPass(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/modify_password.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)
					} else {
						uni.showToast({
							title: '' + res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				}).catch(err => {
					uni.showToast({
						title: '' + res.data.msg,
						icon: 'none'
					});
					reject(res)
				})
			})
		},
		/**
		 * @description 退出登录
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiLogOut(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/common/logout.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						uni.removeStorageSync("token");
						this.commit("userInfo", null);
						this.commit("token", null);
						this.commit("isLogin", false);
						resolve(res)
					} else {
						reject(res)
					}
				}).catch(err => {
					uni.showToast({
						title: '用户信息修改失败,' + res.data.msg,
						icon: 'none'
					});
					reject(res)
				})
			})
		},
		/**
		 * @description 修改用户信息
		 * @param {Object} context
		 * @param {Object} _data
		 */
		eidtUserInfo(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/update_info.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)
					} else {
						uni.showToast({
							title: '用户信息修改失败,' + res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				}).catch(err => {
					uni.showToast({
						title: '用户信息修改失败,' + res.data.msg,
						icon: 'none'
					});
					reject(res)
				})
			})
		},
		/**
		 * @description 修改手机号
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiEditTel(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/update_phone.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)
						this.commit("eidtTel", _data.phone)
					} else {
						uni.showToast({
							title: '修改失败,' + res.data.msg,
							icon: 'none'
						});
						reject(res)
					}
				}).catch(err => {
					uni.showToast({
						title: '修改失败,' + res.data.msg,
						icon: 'none'
					});
					reject(res)
				})
			})
		},
		loginByWexin(context) {
			return new Promise((resolve, reject) => {
				wx.login({
					success: (res) => {
						if (res.code) {			//微信登录成功 已拿到code
							let _url='/common/unionId.do?js_code='+res.code
							Request().request({
								url: _url,
								method: 'get'
							}).then(session_res => {
								resolve(session_res)
							}).catch(err => {
								reject(err)
							})
						} else {
							reject(res)
							console.log('登录失败！' + res.errMsg)
						}
					}
				})
			})

		},
		loginByQQ(context) {
			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'qq',
					success: (loginRes) => {
						
						uni.getUserInfo({
							provider: 'qq',
							success: (infoRes) => {
								resolve(infoRes);
							}
						});
					}
				});
			})

		},
		loginByWeiBo(context) {
			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'sinaweibo',
					success: (loginRes) => {
						
						uni.getUserInfo({
							provider: 'sinaweibo',
							success: (infoRes) => {
								resolve(infoRes);
							},
							fail: (err) => {
								console.log(err);
							}
						});
					}
				});
			})

		},
		/**
		 * 通过APPLE 登录
		 * @param {Object} context
		 */
		loginByApple(context) {

			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'apple',
					success: (loginRes) => {
						
						uni.getUserInfo({
							provider: 'apple',
							success: (infoRes) => {
								
								resolve(infoRes);
							},
							fail: (err) => {
								console.log(err);
							}
						})
					},
					fail: (err) => {
						console.log(err);
					}
				});
			})

		},
		/**
		 * @description 第三方登录
		 * @param {Object} context
		 * @param {Object} _data
		 * loginType:1微信   2qq   3微博  4apple
		 */
		apiThirdLogin(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/common/mpRegist.do',
					method: 'post',
					data: _data
				}).then(res => {				
					if (res.data.code == 1) {
						this.commit("userInfo", res.data.data);
						this.commit("token", res.data.data.token);
						this.commit("isLogin", true);
						resolve(res)
					} else {
						reject(res)
					}

				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @description 门店统计
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiPostTongji(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/store/stat/add.do',
					method: 'post',
					data: _data
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * 获取家庭成员列表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetMemberList(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/member/list.do',
					method: 'post',
					data: _data
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**添加学员
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiAddMember(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/member/add.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)
					} else {
						uni.showToast({
							title: '添加失败',
							icon: 'none'
						});
						reject(res)
					}
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**修改学员
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiEditMember(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/member/update.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)
					} else {
						uni.showToast({
							title: '修改失败',
							icon: 'none'
						});
						reject(res)
					}
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**删除学员
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiRemoveMember(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/member/update.do',
					method: 'post',
					data: _data
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @description 绑定支付宝
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiBindAlipay(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/group/aliBind.do',
					method: 'post',
					data: _data
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**提现
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiWithdrawal(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/group/fetch.do',
					method: 'post',
					data: _data
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @description 获取我的关注
		 * @param {Object} context
		 * @param {Object} _data+
		 */
		apiGetMyCollection(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/user/fav/list.do',
					method: 'post',
					data: _data
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**
		 * @description 意见反馈
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiFeedBack(context, _data) {
			return new Promise((resolve, reject) => {
				Request().request({
					url: '/common/feedback/add.do',
					method: 'post',
					data: _data
				}).then(res => {
					resolve(res)
				}).catch(err => {
					reject(res)
				})
			})
		},
		/**获取消息列表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetNociteList(context, _data) {
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/msg/list.do',
					method: 'post',
					data: _data
				}).then(res => {
					if (res.data.code == 1) {
						resolve(res)

					} else {
						uni.showToast({
							title: '消息列表获取失败,' + res.data.msg,
							icon: 'none'
						});
					}
				})

			})
		},
		/**
		 * @description  获取我的优惠券列表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetMyCouponList(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/coupon/user/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
						
					}else{
						uni.showToast({
							title: '优惠券列表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description 获取课领取优惠券列表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetCatchCouponList(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/coupon/get/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
						
					}else{
						uni.showToast({
							title: '可领取优惠券列表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description 领取新人礼包
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetNewGift(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/coupon/regist/fetch.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						uni.showToast({
							title: '领取成功',
							icon: 'none'
						});
						resolve(res)
						
					}else{
						uni.showToast({
							title: '领取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description 获取学员成长记录
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetMemberLog(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/memberlog/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
						
					}else{
						uni.showToast({
							title: '优惠券列表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description  新增/编辑学员成长记录
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiPostMemberLog(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/memberlog/save.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
						
					}else{
						uni.showToast({
							title: '保存失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description 删除学员成长记录
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiDelMemberLog(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/memberlog/delete.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
						
					}else{
						uni.showToast({
							title: '删除记录失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description 获取消息总数
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetUnreadMsg(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/msg/stat.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
					
						resolve(res)
						
					}else{
						uni.showToast({
							title: '获取消息总数失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**获取课表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetClassTable(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/classtable/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
					
						resolve(res)
						
					}else{
						uni.showToast({
							title: '课表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * 获取某一天的点名状态
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetRollStatus(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/timeable/stat.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
					
						resolve(res)
						
					}else{
						uni.showToast({
							title: '点名状态获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * 获取考勤签到学员
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetRollMember(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/timeable/student.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
					
						resolve(res)
						
					}else{
						uni.showToast({
							title: '获取签到学员失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * 获取用户课次列表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetClassTimeList(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/user/orderTime/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
					
						resolve(res)
						
					}else{
						uni.showToast({
							title: '课次列表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * 获取用户点名记录
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetRollRecord(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/checkin/listByOrderId.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
					
						resolve(res)
						
					}else{
						uni.showToast({
							title: '课次列表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * 获取学员可请假课程
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetMemberClass(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/leave/order/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
					}else{
						uni.showToast({
							title: '可请假课程获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description 申请请假
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiApplyLeave(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/leave/add.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.code==1){
						resolve(res)
					}else{
						uni.showToast({
							title: '申请请假失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * @description 
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetLeaveList(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/leave/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.result==1){
						resolve(res)
					}else{
						uni.showToast({
							title: '请假列表获取失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**加入团队
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiJoinTeam(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/agent/partner.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.result==1){
						resolve(res)
					}else{
						uni.showToast({
							title: '加入团队失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		
		/** 招商
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiJoinTeamb(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: 'group/myInvestment.do',
					method: 'GET',
					data: _data
				}).then(res=>{
					
						resolve(res)
				})
				
			})
		},
		/**获取我的团队成员
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetTeamList(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/agent/listMember.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.result==1){
						resolve(res)
					}else{
						uni.showToast({
							title: '获取团队列表失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * 获取分销佣金列表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetDistionComm(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/agent/award/list.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.result==1){
						resolve(res)
					}else{
						uni.showToast({
							title: '获取团队列表失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		},
		/**
		 * 获取招商分销佣金列表
		 * @param {Object} context
		 * @param {Object} _data
		 */
		apiGetDistionCommzhaoshang(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/group/merchantsBrokerage.do',
					method: 'GET',
					data: _data
				}).then(res=>{
					console.log(res)
					
					resolve(res)
					
				})
				
			})
		},
		/**
		 * @description 解密获取用户手机号
		 * @param {Object} context
		 * @param {Object} _data
		 */
		getWxPhone(context,_data){
			return new Promise(resolve => {
				let r = Request();
				r.request({
					url: '/common/decryptPhone.do',
					method: 'post',
					data: _data
				}).then(res=>{
					if(res.data.result==1){
						let _data=JSON.parse(res.data.data)
						resolve(_data)
					}else{
						uni.showToast({
							title: '解密用户信息失败,'+res.data.msg,
							icon: 'none'
						});
					}
				})
				
			})
		}
	}
}

export default user
